package com.dt.global.web.action.linksquery;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.annotation.Scope;
import com.dt.components.exstruts.action.BaseActionSupport;
import com.dt.global.entitys.bo.TWebLinks;
import com.dt.global.utils.Proerties;
import com.dt.global.web.mar.linksquery.LinksQueryMar;

/**
 * 友情链接的action
 * @author MWG
 *
 */
@SuppressWarnings("serial")
@Scope("prototype")
public class LinksQueryAction  extends BaseActionSupport{
	//json解析对象
	public ObjectMapper mapper=new ObjectMapper();
	@Resource
	public LinksQueryMar linksQueryMar;
	/**
	 * 根据栏目id查询友情链接(ajax)
	 */
	public String getTWebLinksQueryAjax(){
		HttpServletResponse resp = 
			ServletActionContext.getResponse();
		resp.setContentType("text/html;utf-8");
		resp.setCharacterEncoding("utf-8");
		try {
			String channelId = getRequest().getParameter("channelId");
			Proerties popUtil = new Proerties();
			String url = popUtil.getValue("wjrdht");
			if(channelId !=null&!channelId.equals("")){
				List<Map<String,String>> list = new ArrayList<Map<String,String>>();
				List<TWebLinks> Links= linksQueryMar.getTWebLinksQuery(channelId);
				//声明Map对象
				Map<String,String> map = null;
				if(Links.size()>0){
					for(TWebLinks links : Links){
						map = new HashMap<String, String>();
						map.put("linksId", links.getLinksId());
						map.put("channelId", channelId);
						map.put("title", links.getTitle());
						if(links.getYl2()!=null&&links.getYl2().length()>0){
						map.put("titleImg",url+"/uploadfile/"+links.getYl2());
						}else{
						map.put("titleImg","");	
						}
						if(links.getUrl().contains("http://")){
							map.put("url",links.getUrl());	
						}else{
							map.put("url","http://"+links.getUrl());	
						}
						list.add(map);
					}
				}
			
				String json = mapper.writeValueAsString(list);
				PrintWriter out  =resp.getWriter();
				out.print(json);
				out.flush();
				out.close();
				return null;
			}else{
				return "error";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	/**
	 * 根据栏目id查询友情链接
	 */
	public String getTWebLinksQuery(){
		HttpServletResponse resp = 
			ServletActionContext.getResponse();
		resp.setContentType("text/html;utf-8");
		resp.setCharacterEncoding("utf-8");
		try {
			Proerties popUtil = new Proerties();
			String url = popUtil.getValue("wjrdht");
			String channelId = getRequest().getParameter("channelId");
			if(channelId !=null&!channelId.equals("")){
				List<Map<String,String>> list = new ArrayList<Map<String,String>>();
				List<TWebLinks> Links= linksQueryMar.getTWebLinksQuery(channelId);
				//声明Map对象
				Map<String,String> map = null;
				if(Links.size()>0){
					for(TWebLinks links : Links){
						if(links.getUrl()!=null&&links.getUrl().contains("http://")){
							links.setUrl(links.getUrl());
						}else{
							links.setUrl("http://"+links.getUrl());
						}
						if(links.getYl2()!=null&&links.getYl2().length()>0){
							links.setYl2(url+"/uploadfile/"+links.getYl2());
						}else{
							links.setYl2("");
						}
						list.add(map);
					}
				}
				getRequest().setAttribute("links", Links);
				return "success";
			}else{
				return "error";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
}
